﻿using Newtonsoft.Json;
using SuperX.Common.Helper;
using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.SuperSocket.Socket.Message
{
    public class SocketMessage
    {
        public const byte FixHeaderLength = 13;
        private string _command = "SendObject";
        private string _header = string.Empty;

        public byte[] FixHeader { set; get; } = new byte[] { 67, 81, 67, 81 }; // ASCII固定编码

        /// <summary>
        /// 消息版本号
        /// </summary>
        public byte Version { get; set; } = 01;

        /// <summary>
        /// 最大长度255(0xFF)
        /// </summary>
        public string Command
        {
            get => _command;
            set
            {
                if (value != null)
                {
                    _command = value.Length > 255 ? value.Substring(0, 255) : value;
                }
            }
        }

        /// <summary>
        /// 最大长度16777215（0xFFFFFF）
        /// </summary>
        [Obsolete("Header 已作废,请使用Headers替代")]
        public string Header
        {
            get => _header ?? string.Empty;
            set
            {
                if (value != null)
                {
                    _header = value.Length > 0xFFFFFF ? value.Substring(0, 0xFFFFFF) : value;
                }
            }
        }

        public Dictionary<string, string> Headers { get; set; }

        /// <summary>
        /// Body长度
        /// </summary>
        public uint BodyLength
        {
            get
            {
                return (uint)(Body == null ? 0 : Body.Length);
            }
        }

        public bool IsError { get; set; }

        public string Error { get; set; }

        public byte[] Body { get; set; }

        public virtual byte[] GetSendBytes()
        {
            var result = new List<byte>();
            result.AddRange(FixHeader);
            result.Add(Version);
            var commandBytes = Encoding.UTF8.GetBytes(Command);
            var headerBytes = Encoding.UTF8.GetBytes(Header);
            var headerLength = (uint)(headerBytes.Length * 256 + commandBytes.Length);
            result.AddRange(BitConverter.GetBytes(headerLength));
            result.AddRange(BitConverter.GetBytes(BodyLength));
            result.AddRange(commandBytes);
            result.AddRange(headerBytes);
            if (Body?.Length > 0)
            {
                result.AddRange(Body);
            }
            return result.ToArray();
        }
    }

    public class SocketMessage<T> : SocketMessage
    {
        private T _connect;

        /// <summary>
        ///  消息格式
        /// </summary>
        [JsonIgnore]
        public T Content
        {
            get => _connect;
            set
            {
                if (value != null && (_connect == null || !_connect.Equals(value)))
                {
                    Body = Encoding.UTF8.GetBytes(JsonHelper.TransferObjectJsonSerialize(value));
                    _connect = value;
                }
            }
        }
    }
}